package com.pet.service;

import com.pet.bean.Account;
import com.pet.bean.Owner;
import com.pet.bean.Pet;
import com.pet.bean.Petstore;
import com.pet.dao.AccountDao;
import com.pet.dao.OwnerDao;
import com.pet.dao.PetDao;
import com.pet.dao.PetstoreDao;
import com.pet.util.DataSourcesTool;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;


//学习过程中的文件，最后项目不需要

public class PetService {
    //创建数据库类的对象
    //建议放在外面，因为可以有多个方法要用
    PetDao petDao = new PetDao();
    OwnerDao ownerDao = new OwnerDao();
    PetstoreDao petStoreDao = new PetstoreDao();
    AccountDao accountDao = new AccountDao();

    //主人登录
    public Owner ownerLogin(String name, String pw) {
        Connection con = DataSourcesTool.getConnection();
        try {
            return ownerDao.login_Owner(con, name, pw);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return null;
    }
    //购买宠物
    public void ownerBuyPet(int pet_id, int owner_id) {
        Connection con = DataSourcesTool.getConnection();
        try {
            //关闭自动提交
            con.setAutoCommit(false);
            //先查询出库存宠物
            List<Pet> pets = petDao.select_AllPet(con);
            System.out.println("商店库存有：");
            for (Pet pet : pets) {
                System.out.println(pet);
            }
            // (1)扣除宠物主人金额【宠物的金额】
            // --根据pet_id把宠物对象查询出来
            Pet pet = petDao.select_PetByID(con, pet_id);
            //如果商店id为空，则说明没有该宠物
            if (pet.getStore_id() == null || pet.getStore_id() == 0) {
                throw new SQLException("商店id为空，无法购买");
            }
            // --根据owner_id修改主人的金额,扣除金额
            ownerDao.update_OwnerWealth1(con, owner_id, pet.getWorth());

            // (2)添加宠物商店余额
            petStoreDao.update_PetstoreWealth1(con, pet.getStore_id(), pet.getWorth());
            // (3)添加帐户表一条记录(account表) type=1表示主人买宠物
            Account account = new Account(pet.getStore_id(), owner_id, 1, null, pet.getWorth(), pet.getPet_id());
//            account.setSale_id(pet.getStore_id());
//            account.setBuy_id(owner_id);
//            account.setType(1);//出售人为商家
//            account.setPrice(pet.getWorth());
//            account.setPet_id(pet.getPet_id());

            accountDao.insert_Account(con, account);
            // (4)更新宠物表，store_id设为null,owner_id是主人ID
            petDao.updatePetOwner(con, null, owner_id, pet_id);
        } catch (SQLException sqlException) {
            try {
                con.rollback();
                System.out.println("回滚");
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            sqlException.printStackTrace();
        }
        try {
            con.commit();
            //最后关闭链接
            DataSourcesTool.close(con);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
    }
    //卖出宠物
    public void ownerSellPet(int pet_id, int store_id,int o_id) {
        Connection con = DataSourcesTool.getConnection();
        try {
            con.setAutoCommit(false);
            //先查询出当前登录主人的宠物
            List<Pet> pets = petDao.select_PetByOwner_id(con, o_id);
            System.out.println("你的宠物有：");
            for (Pet pet : pets) {
                System.out.println(pet);
            }
            List<Petstore> petstores = petStoreDao.selectAllStore(con);
            System.out.println("商家有：");
            for (Petstore petstore : petstores) {
                System.out.println(petstore);
            }
            // (1)指定卖出的宠物，以及售予的商家(查出商家列表)
            Pet pet = petDao.select_PetByID(con, pet_id);
            //如果商店id为空，则说明没有该宠物
            if (pet.getOwner_id() == null || pet.getOwner_id() == 0) {
                throw new SQLException("主人id为空，无法购买");
            }
            Petstore petstore = petStoreDao.select_PetstoreByID(con, store_id);
            // (2)添加宠物主人金额
            ownerDao.update_OwnerWealth2(con, pet_id, pet.getWorth());
            // (3)扣除宠物商店余额
            petStoreDao.update_PetstoreWealth2(con, store_id, pet.getWorth());
            // (4)添加帐户表一条记录(account表) type=2表示主人卖宠物
            Account account = new Account(pet.getOwner_id(), store_id, 2, null, pet.getWorth(), pet.getPet_id());
            accountDao.insert_Account(con, account);
            // (5)更新宠物表，store_id设为商店id,owner_id设为null
            petDao.updatePetOwner(con, store_id, null, pet_id);
        } catch (SQLException sqlException) {
            try {
                con.rollback();
                System.out.println("回滚");
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            sqlException.printStackTrace();
        }
        try {
            con.commit();
            DataSourcesTool.close(con);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
    }
    //商店登录
    public Petstore petstoreLogin(String name,String pw) {
        Connection con = DataSourcesTool.getConnection();
        try {
            return petStoreDao.login_Petstore(con,name,pw);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        DataSourcesTool.close(con);
        return null;
    }
    //购买宠物
    public void storeBuyPet(int pet_id,int store_id,int o_id) {
        Connection con = DataSourcesTool.getConnection();
        try {
            //关闭自动提交
            con.setAutoCommit(false);
            //先查询出库存宠物
            List<Pet> pets = petDao.select_PetByOwner_id(con,o_id);
            System.out.println("主人代售有：");
            for (Pet pet : pets) {
                System.out.println(pet);
            }
            // (1)扣除商店金额【宠物的金额】
            // --根据pet_id把宠物对象查询出来
            Pet pet = petDao.select_PetByID(con, pet_id);
            //如果商店id为空，则说明没有该宠物
            if (pet.getOwner_id() == null || pet.getOwner_id() == 0) {
                throw new SQLException("主人id为空，无法购买");
            }
            // --根据owner_id修改主人的金额,增加金额
            ownerDao.update_OwnerWealth2(con,pet.getOwner_id(), pet.getWorth());
            // (2)减少宠物商店余额
            petStoreDao.update_PetstoreWealth2(con, store_id, pet.getWorth());
            // (3)添加帐户表一条记录(account表) type=3表示商店买宠物
            Account account = new Account(pet.getOwner_id(), store_id, 3, null, pet.getWorth(), pet.getPet_id());
            accountDao.insert_Account(con, account);
            // (4)更新宠物表
            petDao.updatePetOwner(con, store_id, null, pet_id);
        } catch (SQLException sqlException) {
            try {
                con.rollback();
                System.out.println("回滚");
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            sqlException.printStackTrace();
        }
        try {
            con.commit();
            //最后关闭链接
            DataSourcesTool.close(con);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
    }
    //卖出宠物
    public void storeSellPet(int pet_id, int owner_id,int s_id) {
        Connection con = DataSourcesTool.getConnection();
        try {
            con.setAutoCommit(false);
            //先查询出当前登录商家的宠物
            List<Pet> pets = petDao.select_PetByStore_id(con, s_id);
            System.out.println("你的宠物有：");
            for (Pet pet : pets) {
                System.out.println(pet);
            }
            List<Owner> owners = ownerDao.selectOwner(con);
            System.out.println("主人有：");
            for (Owner owner : owners) {
                System.out.println(owner);
            }
            // (1)指定卖出的宠物，以及售予的商家(查出商家列表)
            Pet pet = petDao.select_PetByID(con, pet_id);
            //如果商店id为空，则说明没有该宠物
            if (pet.getStore_id() == null || pet.getStore_id() == 0) {
                throw new SQLException("商店id为空，无法购买");
            }
            Owner owner = ownerDao.select_OwnerByID(con, owner_id);
            // (2)减少宠物主人金额
            ownerDao.update_OwnerWealth1(con, pet_id, pet.getWorth());
            // (3)增加宠物商店余额
            petStoreDao.update_PetstoreWealth1(con, s_id, pet.getWorth());
            // (4)添加帐户表一条记录(account表) type=4表示商店卖宠物
            Account account = new Account(s_id, owner_id, 4, null, pet.getWorth(), pet.getPet_id());
            accountDao.insert_Account(con, account);
            // (5)更新宠物表
            petDao.updatePetOwner(con, null, owner_id, pet_id);
        } catch (SQLException sqlException) {
            try {
                con.rollback();
                System.out.println("回滚");
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            sqlException.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            con.commit();
            DataSourcesTool.close(con);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
    }
    //培育宠物
    public void petAdd(String name,Object s_id,Object o_id,double worth,String type){
        Connection con = DataSourcesTool.getConnection();
        try {
            petDao.createPet(con,name,s_id,o_id,worth,type);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        DataSourcesTool.close(con);
    }
    // 查询待售宠物 --查询pet表，owner_id为null的宠物
    public void selectPet(int s_id) {
        Connection con = DataSourcesTool.getConnection();
        try {
            List<Pet> pets = petDao.select_PetByPetstore(con,s_id);
            for (Pet pet : pets) {
                System.out.println(pet);
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        DataSourcesTool.close(con);
    }
    //查询商店余额
    public void selectStoreWealth(int s_id){
        Connection con = DataSourcesTool.getConnection();
        try {
            double wealth = petStoreDao.selectStoreWealth(con, s_id);
            if (wealth==0){
                System.out.println("商店id错误");
            } else {
                System.out.println("余额为"+wealth);
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
    }
    // 开宠物商店  --添加宠物商店记录(petstore表)
    public void addPetStore(Petstore p){
        Connection con = DataSourcesTool.getConnection();
        try {
            boolean b = petStoreDao.openPetStore(con, p);
            if (b==true){
                System.out.println("创建成功");
            }else {
                System.out.println("创建失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
    }
}
